phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file view.php

Documentation is available at view.php

  1. <?php
  2. /**
  3.  * This file groups classes pertaining to the "view" part of MVC.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  */
  9.  
  10. /**
  11.  * Holds several slots.
  12.  * A slot is a cross-template variable, like a block, a title or even an object.
  13.  * 
  14.  * @since 0.1
  15.  */
  16. class SlotHolder {
  17.     /**
  18.      * Contains slots indexed by names.
  19.      * 
  20.      * @var array 
  21.      */
  22.     private $slots array();
  23.     
  24.     /**
  25.      * The name of the slot that has been started with
  26.      * {@link SlotHolder::start()}. Null if no slot is open.
  27.      * 
  28.      * @var string 
  29.      */
  30.     private $open Null;
  31.     
  32.     /**
  33.      * Adds a slot.
  34.      * Slot $name must not exist.
  35.      * 
  36.      * @param string $name name for this slot
  37.      * @param mixed $value value for this slot
  38.      * @since 0.1
  39.      */
  40.     public function add($name$value{
  41.         $this->crashIfExists($name);
  42.         
  43.         $this->slots[$name$value;
  44.     }
  45.     
  46.     /**
  47.      * Returns True if a slot is opened.
  48.      * 
  49.      * @return bool 
  50.      * @since 0.1
  51.      */
  52.     public function isOpen({
  53.         return $this->open !== Null;
  54.     }
  55.     
  56.     /**
  57.      * Throws a SlotException if slot $name exists.
  58.      * 
  59.      * @throws SlotException
  60.      * @since 0.1
  61.      */
  62.     public function crashIfExists($name{
  63.         if ($this->exist($name)) {
  64.             throw new SlotException(
  65.                 'Caught attempt to duplicate slot "' $name '"');
  66.         }
  67.     }
  68.     
  69.     /**
  70.      * If a slot is open, terminates the output buffer and throws a
  71.      * SlotException.
  72.      * 
  73.      * @throws SlotException
  74.      * @since 0.1
  75.      */
  76.     public function crashIfOpen({
  77.         if ($this->isOpen()) {
  78.             $this->terminateBuffer();
  79.             throw new SlotException(
  80.                 'Slot "' $this->open '" has not been closed.');
  81.         }
  82.     }
  83.     
  84.     /**
  85.      * Opens a slot. Its contents will be the text printed until a call to
  86.      * {@link SlotHolder::close()} is issued.
  87.      * No slot must be opened, and slot $name must not exist.
  88.      * 
  89.      * @param string $name the name of this slot
  90.      * @since 0.1
  91.      */
  92.     public function open($name{
  93.         $this->crashIfOpen();
  94.         
  95.         $this->open $name;
  96.         ob_start();
  97.     }
  98.     
  99.     /**
  100.      * Stores the previously opened slot. Throws a SlotException if no slot is
  101.      * open.
  102.      * 
  103.      * @throws SlotException
  104.      * @since 0.1
  105.      */
  106.     public function close({
  107.         if (!$this->isOpen()) {
  108.             throw new SlotException('No slot is open.');
  109.         }
  110.         
  111.         $this->slots[$this->openob_get_contents();
  112.         $this->terminateBuffer();
  113.     }
  114.     
  115.     /**
  116.      * Terminates the buffer and resets $open to Null.
  117.      * This must not be called unless a buffer has been opened by this template.
  118.      */
  119.     private function terminateBuffer({
  120.         ob_end_clean();
  121.         $this->open Null;
  122.     }
  123.     
  124.     /**
  125.      * Returns the value of slot $name if it exists,
  126.      * the default value otherwise.
  127.      * 
  128.      * @param string $name 
  129.      * @param mixed $default 
  130.      * @return mixed 
  131.      * @since 0.1
  132.      */
  133.     public function getOrDefault($name$default{
  134.         return array_get_default($this->slots$name$default);
  135.     }
  136.     
  137.     /**
  138.      * Returns the slot $name.
  139.      * Throws a SlotException if this slot does not exist.
  140.      * 
  141.      * @param string $name 
  142.      * @throws SlotException
  143.      * @return mixed 
  144.      * @since 0.1
  145.      */
  146.     public function getOrCrash($name{
  147.         if (array_key_exists($name$this->slots)) {
  148.             return $this->slots[$name];
  149.         }
  150.         else {
  151.             throw new SlotException(
  152.                 'Required slot "' $name '" is missing.');
  153.         }
  154.     }
  155.     
  156.     /**
  157.      * Returns True if slot $name exists, False otherwise.
  158.      * 
  159.      * @param string $name 
  160.      * @return bool 
  161.      * @since 0.1
  162.      */
  163.     public function exist($name{
  164.         return array_key_exists($name$this->slots|| $this->open === $name;
  165.     }
  166. }
  167.  
  168. /**
  169.  * Renders an associative array.
  170.  * A template can be used as many times as needed.
  171.  * 
  172.  * @since 0.1
  173.  */
  174. interface Template {
  175.     /**
  176.      * Renders the given object/value.
  177.      * $slots is injected in the template as '$slots'.
  178.      * 
  179.      * @param mixed $object 
  180.      * @since 0.1
  181.      */
  182.     function render($varsSlotHolder $slots=Null);
  183. }
  184.  
  185. /**
  186.  * Represents a template described by a PHP/HTML file.
  187.  * 
  188.  * @since 0.1
  189.  */
  190. class FileBasedTemplate implements Template {
  191.     private $filename;
  192.     
  193.     /**
  194.      * @param string $filename path to template file
  195.      */
  196.     public function __construct($filename{
  197.         if (is_file($filename)) {
  198.             $this->filename $filename;
  199.         }
  200.         else {
  201.             throw new InvalidArgumentException('No such template: '$filename);
  202.         }
  203.     }
  204.     
  205.     /**
  206.      * (non-PHPdoc)
  207.      * @see WebCommons/Template::render()
  208.      */
  209.     public function render($varsSlotHolder $slots=Null{
  210.         ob_start();
  211.         extract($vars);
  212.         include($this->filename);
  213.         return ob_get_clean();
  214.     }
  215. }
  216.  
  217. /**
  218.  * Exception thrown when an errors occurs with slots.
  219.  * 
  220.  * @since 0.1
  221.  */
  222. class SlotException extends Exception {}

Documentation generated on Fri, 16 Jul 2010 00:48:40 +0200 by phpDocumentor 1.4.3